package com.dy.drp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.drp.dto.BuyinfoDto;
import com.dy.drp.dto.ClientCollect;
import com.dy.drp.entity.Buyinfo;
import com.dy.drp.entity.Saleinfo;
import com.dy.drp.mapper.SaleinfoMapper;
import com.dy.drp.mapper.SalelistMapper;
import com.dy.drp.service.ISaleinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.drp.util.PageUtil;
import com.dy.drp.util.SelectUtil;
import com.dy.drp.vo.SaleinfoVo;
import com.dy.drp.vo.SalelistVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dy
 * @since 2022-04-02
 */
@Service
public class SaleinfoServiceImpl extends ServiceImpl<SaleinfoMapper, Saleinfo> implements ISaleinfoService {
    @Autowired
    private SaleinfoMapper saleinfoMapper;
    @Autowired
    private SalelistMapper salelistMapper;

    @Override
    public PageUtil<SaleinfoVo> selectOnePageSaleinfo(PageUtil<SaleinfoVo> pageUtil) {
        List<SaleinfoVo> saleinfoVos = saleinfoMapper.selectOnePageSaleinfo(pageUtil);
        pageUtil.setList(saleinfoVos);
        Integer integer = saleinfoMapper.selectCount(null);
        pageUtil.setCount(integer);
        pageUtil.setTotalPage(integer);
        return pageUtil;
    }

    @Override
    public PageUtil<SaleinfoVo> selectByClientAndTime(SelectUtil selectUtil) {
        PageUtil<SaleinfoVo> pageUtil = new PageUtil<>();
        pageUtil.setPageSize(selectUtil.getPageSize());
        pageUtil.setPageNum(selectUtil.getPageNum());

        QueryWrapper<Saleinfo> wrapper = new QueryWrapper<>();
        wrapper.inSql("client_id","select client_id from t_client where client_name like '%"+selectUtil.getName()+"%'");
        wrapper.between("sale_time",selectUtil.getStartTime(),selectUtil.getEndTime());
        Integer integer = saleinfoMapper.selectCount(wrapper);
        pageUtil.setCount(integer);
        pageUtil.setTotalPage(integer);

        List<SaleinfoVo> saleinfoVos = saleinfoMapper.selectOnePageSaleinfoByinfo(selectUtil);
        pageUtil.setList(saleinfoVos);
        return pageUtil;
    }

    @Override
    public SaleinfoVo selectOneSaleinfoById(Long saleId) {
        SaleinfoVo saleinfoVo = saleinfoMapper.selectOneSaleinfoById(saleId);
        List<SalelistVo> salelistVos = salelistMapper.selectSaleListBySaleId(saleId);
        saleinfoVo.setList(salelistVos);
        return saleinfoVo;
    }

    @Override
    public PageUtil<ClientCollect> clientCollect(PageUtil<ClientCollect> pageUtil) {
        Integer integer = saleinfoMapper.clientCollectCount().size();
        pageUtil.setCount(integer);
        pageUtil.setTotalPage(integer);

        List<ClientCollect> clientCollects = saleinfoMapper.clientCollect(pageUtil);
        pageUtil.setList(clientCollects);
        return pageUtil;
    }

    @Override
    public PageUtil<ClientCollect> clientCountByInfo(SelectUtil selectUtil) {
        PageUtil<ClientCollect> pageUtil = new PageUtil<>();
        pageUtil.setPageNum(selectUtil.getPageNum());
        pageUtil.setPageSize(selectUtil.getPageSize());

        Integer integer = saleinfoMapper.clientCollectCountByInfo(selectUtil).size();
        pageUtil.setCount(integer);
        pageUtil.setTotalPage(integer);

        List<ClientCollect> clientCollects = saleinfoMapper.clientCollectByInfo(selectUtil);
        pageUtil.setList(clientCollects);

        return pageUtil;
    }

    @GetMapping("/clientCollect/{pageNum}/{pageSize}")
    @ApiOperation("客户统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value="当前页码"),
            @ApiImplicitParam(name="pageSize",value="页码展示条数")
    })
    public PageUtil<ClientCollect> clientCollect(@PathVariable("pageNum")Integer pageNum,
                                                 @PathVariable("pageSize")Integer pageSize){
        PageUtil<ClientCollect> pageUtil = new PageUtil<>();
        pageUtil.setPageNum(pageNum);
        pageUtil.setPageSize(pageSize);

        List<ClientCollect> clientCollects = saleinfoMapper.clientCollect(pageUtil);
        pageUtil.setList(clientCollects);

        Integer integer = saleinfoMapper.clientCollectCount().size();
        pageUtil.setCount(integer);
        pageUtil.setTotalPage(integer);
        return pageUtil;
    }


}
